<?php

namespace App\Http\Controllers;

use App\Models\CustomerAddress;
use App\Models\Recharge;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CustomerAddressController extends Controller
{
    /**
     * 列表
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        $params = $request->all();

        if (empty($params['customer_id'])) {
            return response()->json(['status' => 0, 'message' => '用户ID不能为空']);
        }

        $customerAddress = new CustomerAddress();
        $data = $customerAddress->getList($params);

        return response()->json(['status' => 1, 'data' => $data]);
    }

    /**
     * 新增
     * @param Request $request
     * @return JsonResponse
     */
    public function save(Request $request): JsonResponse
    {
        $params = $request->all();

        if (empty($params['customer_id'])) {
            return response()->json(['status' => 0, 'message' => '用户ID不能为空']);
        }
        if (empty($params['name'])) {
            return response()->json(['status' => 0, 'message' => '联系人不能为空']);
        }
        if (empty($params['phone'])) {
            return response()->json(['status' => 0, 'message' => '联系人电话不能为空']);
        }
        if (empty($params['pathid'])) {
            return response()->json(['status' => 0, 'message' => '省市区不能为空']);
        }
        if (empty($params['address'])) {
            return response()->json(['status' => 0, 'message' => '详细地址不能为空']);
        }
        if (empty($params['six'])) {
            $params['six'] = 1;
        }
        $params['status'] = 'NORMAL';

        CustomerAddress::create($params);

        return response()->json(['status' => 1, 'data' => [], 'message' => '新增成功']);
    }

    /**
     * 更新
     * @param Request $request
     * @return JsonResponse
     */
    public function update(Request $request): JsonResponse
    {
        $params = $request->all();
        if (empty($params['id'])) {
            return response()->json(['status' => 0, 'message' => '收货地址ID不能为空']);
        }
        $address = CustomerAddress::where('id', $params['id'])->first();
        if (empty($address)) {
            return response()->json(['status' => 0, 'message' => '收货地址不存在']);
        }
        if (empty($params['customer_id'])) {
            return response()->json(['status' => 0, 'message' => '用户ID不能为空']);
        }
        if (empty($params['name'])) {
            return response()->json(['status' => 0, 'message' => '联系人不能为空']);
        }
        if (empty($params['phone'])) {
            return response()->json(['status' => 0, 'message' => '联系人电话不能为空']);
        }
        if (empty($params['pathid'])) {
            return response()->json(['status' => 0, 'message' => '省市区不能为空']);
        }
        if (empty($params['address'])) {
            return response()->json(['status' => 0, 'message' => '详细地址不能为空']);
        }
        if (empty($params['six'])) {
            $params['six'] = 1;
        }

        CustomerAddress::where('id', $params['id'])->update($params);

        return response()->json(['status' => 1, 'data' => [], 'message' => '更新成功']);
    }
}
